package com.apobates.forum.utils.lang;

import java.util.Collection;
import java.util.Comparator;
import java.util.SortedSet;
import java.util.TreeSet;

/**
 * A TreeSet that ensures it never grows beyond a max size.  
 * <code>last()</code> is removed if the <code>size()</code> 
 * get's bigger then <code>getMaxSize()</code>
 *
 * @param <E>
 * @see
 * https://github.com/apache/lucene-solr/blob/master/solr/core/src/java/org/apache/solr/util/BoundedTreeSet.java
 * @author xiaofanku
 */
public final class BoundedTreeSet<E> extends TreeSet<E> {
    private final static long serialVersionUID = 1L;
    private final int maxSize;// = Integer.MAX_VALUE;
    
    public BoundedTreeSet(int maxSize) {
        super();
        this.maxSize = maxSize;
    }
    
    public BoundedTreeSet(int maxSize, Collection<? extends E> c) {
        super(c);
        this.maxSize = maxSize;
    }
    
    public BoundedTreeSet(int maxSize, Comparator<? super E> c) {
        super(c);
        this.maxSize = maxSize;
    }
    
    public BoundedTreeSet(int maxSize, SortedSet<E> s) {
        super(s);
        this.maxSize = maxSize;
    }
    
    public int getMaxSize() {
        return maxSize;
    }
    
    private void adjust() {
        while (maxSize < size()) {
            remove(last());
        }
    }
    
    @Override
    public boolean add(E item) {
        boolean out = super.add(item);
        adjust();
        return out;
    }
    
    @Override
    public boolean addAll(Collection<? extends E> c) {
        boolean out = super.addAll(c);
        adjust();
        return out;
    }
}